//
//  UseMoreFilterViewController.m
//  CoreImageDemo
//
//  Created by zero on 2017/6/12.
//  Copyright © 2017年 zero. All rights reserved.
//

#import "UseMoreFilterViewController.h"

@interface UseMoreFilterViewController ()
@property (nonatomic,strong) UIImageView* topImageView;
@property (nonatomic,strong) UIImageView* bottomImageView;
@end

@implementation UseMoreFilterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.topImageView.image = [UIImage imageNamed:@"p199012.jpg"];
    [self.view addSubview:self.topImageView];
    
    
    self.bottomImageView.image = [self action_filterImage];
    [self.view addSubview:self.bottomImageView];
    
}

- (UIImage*)action_filterImage{
    
    CIImage* inputImage = [[CIImage alloc]initWithImage:self.topImageView.image];
    CIFilter* filter = [CIFilter filterWithName:@"CISepiaTone"];
    [filter setValue:inputImage  forKey:kCIInputImageKey];
    [filter setValue:[NSNumber numberWithFloat:0.8] forKey:kCIInputIntensityKey];
    
    CIFilter* secondFilter = [CIFilter filterWithName:@"CIColorInvert"];
    //将第一次滤镜的输出图片作为第二层滤镜的输入图片，更多层，也是依次链型连接。
    [secondFilter setValue:filter.outputImage forKey:kCIInputImageKey];
    
    CIImage* outputImage = [secondFilter valueForKey:kCIOutputImageKey];
    
    CIContext* context = [CIContext contextWithOptions:nil];
    CGImageRef ref = [context createCGImage:outputImage fromRect:outputImage.extent];
    UIImage* newImage = [UIImage imageWithCGImage:ref];
    CGImageRelease(ref);
    return newImage;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIImageView*)topImageView{
    if(!_topImageView){
        _topImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)/2.0)];
        _topImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _topImageView;
}

- (UIImageView*)bottomImageView{
    if(!_bottomImageView){
        _bottomImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.topImageView.frame), CGRectGetWidth(self.topImageView.frame), CGRectGetHeight(self.topImageView.frame))];
        _bottomImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _bottomImageView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
